#include "Set.h"
#include "Object.h"

//static const size_t _Set = sizeof(struct Set);
//static const size_t _Object = sizeof(struct Object);

const void * Set = &_Set;


void * add (void * _set, const void * _element)
{
	struct Set * set = _set;
	struct Object * element = (void*)_element;

	assert(set);
	assert(element);

	element->in = set;
	
	++element->count, ++set->count;

	return element;
}

void * find (const void * _set, const void * _element)
{
	const struct Object * element = _element;

	assert(_set);
	assert(element);

	return element->in == _set ? (void *) element : 0;
}

void * drop (void * _set, const void * _element)
{
	struct Set * set = _set;
	struct Object * element = find(set, _element);
	if (element)
	{
		if(--(element->count) == 0)
			element->in = 0;
		--(set->count);
	}
	return element;
}

unsigned count (const void * _set)
{
	const struct Set * set = _set;
	assert(set);
	return set->count;
}


int contains (const void * _set, const void * _element)
{
	return find(_set, _element) != 0;
}



/*------------------------Object------------------*/
//int differ (const void * _a, const void * _b)
//{
//	return _a != _b;
//}


int differ (const void * self, const void * b)
{
	const struct TypeDescriptor * const * cp = self;
	assert(self && * cp && (* cp)->differ);
	return (* cp)->differ(self, b);
}


size_t sizeOf (const void * self)
{
	const struct TypeDescriptor * const * cp = self;
	assert(self && * cp);
	return (* cp)->size;
}